package com.link.play.fit.ai.ui.weightcontrol.ac

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.activity.addCallback
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcWeightControlDailyBinding
import kotlinx.coroutines.launch
import kotlin.math.min

class WeightControlDailyAc : BaseActivity<AcWeightControlDailyBinding>(AcWeightControlDailyBinding::inflate) {


    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[WeighControlViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {

            viewModel.lastUserGuide = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                intent.getSerializableExtra("data", UserGuide::class.java) ?: UserGuide()
            } else {
                @Suppress("DEPRECATION")
                (intent.getSerializableExtra("data") as UserGuide?) ?: UserGuide()
            }
            Log.e(TAG, "lastUserGuide=${viewModel.lastUserGuide}")

            viewModel.getCalculateLastDaily()
            viewModel.lastUserGuide.caloriesDaily = min(viewModel.lastUserGuide.caloriesDaily, binding.seekbarCal.max)
            viewModel.lastUserGuide.proteinDaily = min(viewModel.lastUserGuide.proteinDaily, binding.seekbarProtein.max)
            viewModel.lastUserGuide.carbsDaily = min(viewModel.lastUserGuide.carbsDaily, binding.seekbarCarbs.max)
            viewModel.lastUserGuide.fatsDaily = min(viewModel.lastUserGuide.fatsDaily, binding.seekbarFat.max)


            binding.seekbarCal.progress = viewModel.lastUserGuide.caloriesDaily
            binding.seekbarProtein.progress = viewModel.lastUserGuide.proteinDaily
            binding.seekbarCarbs.progress = viewModel.lastUserGuide.carbsDaily
            binding.seekbarFat.progress = viewModel.lastUserGuide.fatsDaily

            binding.tvCalories.text = viewModel.lastUserGuide.caloriesDaily.toString() + " cal"
            binding.tvProtein.text = viewModel.lastUserGuide.proteinDaily.toString() + " g"
            binding.tvCarbs.text = viewModel.lastUserGuide.carbsDaily.toString() + " g"
            binding.tvFats.text = viewModel.lastUserGuide.fatsDaily.toString() + " g"
        }


        binding.seekbarCal.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.lastUserGuide.caloriesDaily = progress
                binding.tvCalories.text = viewModel.lastUserGuide.caloriesDaily.toString() + " cal"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        binding.seekbarProtein.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.lastUserGuide.proteinDaily = progress
                binding.tvProtein.text = viewModel.lastUserGuide.proteinDaily.toString() + " g"

            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        binding.seekbarCarbs.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.lastUserGuide.carbsDaily = progress
                binding.tvCarbs.text = viewModel.lastUserGuide.carbsDaily.toString() + " g"

            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        binding.seekbarFat.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                viewModel.lastUserGuide.fatsDaily = progress
                binding.tvFats.text = viewModel.lastUserGuide.fatsDaily.toString() + " g"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        onBackPressedDispatcher.addCallback {
            finish()
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }


        binding.tvNext.setOnClickListener {
            when (viewModel.lastUserGuide.weighControlMethod) {
                0 -> {
                    goToAc(FastingGoalAc::class.java) {
                        putExtra("data", viewModel.lastUserGuide)
                    }
                    overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
                }

                1 -> {
                    goToAc(SetMealAc::class.java) {
                        putExtra("data", viewModel.lastUserGuide)
                    }
                    overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
                }
            }

        }
    }

}